/**
 * 注册页面
 */

$('#register').button({
    loadingText: '注册中...'
});

// 学生注册
$(document).ready(function () {
    $('#register').on('click', function() {
        var $this = $(this);

        var account = $('#account').val();
        if (!account) {
            eui.msg('邮箱不可为空');
            return;
        }
        if (!/^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/.test(account)) {
            eui.msg('邮箱格式不正确');
            return;
        }
        var password = $('#password').val();
        if (!password) {
            eui.msg('密码不能为空');
            return;
        }

        if (password.length < 6) {
            eui.msg('密码长度不能小于6位');
            return;
        }

        var password2 = $('#password2').val();
        if (!password2) {
            eui.msg('确认密码不能为空');
            return;
        }

        if (password != password2) {
            eui.msg('两次密码输入不一致');
            return;
        }

        var school = $('#school').val();
        if (!school) {
            eui.msg('请输入学校');
            return;
        }

        var name = $('#name').val();
        if (!name) {
            eui.msg('请输入姓名');
            return;
        }
        if (!/^[\u0391-\uFFE5]*?$/.test(name)) {
            eui.msg('姓名必须是汉字');
            return;
        }

        if (name.length > 4 || name.length < 2) {
            eui.msg('姓名长度不正确');
            return;
        }
        var schoolId = $('#school-id').val();
        if (!schoolId) {
            eui.msg('请输入学号');
            return;
        }
        if (!/^[\d]*?$/.test(schoolId)) {
            eui.msg('学号必须是数字');
            return;
        }
        if (schoolId.length > 20 || schoolId.length < 6) {
            eui.msg('学号长度不正确');
            return;
        }

        $this.button('loading');

        $.ajax({
            url: '/user/enroll',
            data: {
                user: account,
                email: account,
                password: password,
                school: school,
                name: name,
                school_id: schoolId
            },
            type: 'POST',
            dataType: 'json',
            success: function (obj) {
                $this.button('reset');
                if (obj.code === 0) {
                    eui.alert('注册成功，请登陆注册邮箱进行账号激活');
                } else {
                    eui.msg(obj.msg);
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                $this.button('reset');
                eui.msg('系统出错');
            }
        });
    });
});

$('#go-back').on('click', function () {
    window.history.go(-1);
});